package com.wehealth.three.lib_connect.device.bluetoothdevice.kdevice

import android.annotation.SuppressLint
import android.app.Activity
import android.bluetooth.BluetoothDevice
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanFilter
import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.bluetooth.le.ScanSettings.CALLBACK_TYPE_FIRST_MATCH
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
 import com.wehealth.three.lib_connect.device.usbdevice.data.ParseOriginEcgData
import com.wehealth.three.lib_connect.utils.MainHandler

class BleBoothToothHelper(activity: Activity) : BlueToothHelper(activity) {


    private var deviceList = mutableListOf<BluetoothDevice>()


    private var scanFilterList = mutableListOf<ScanFilter>()

    private var bindDeviceList = mutableListOf<BluetoothDevice>()


    private var scanning = false


    private val scanFilter = ScanFilter.Builder()
        .setDeviceName(SCAN_BLE_DEVICE_NAME)
        .build()

    private val scanSettings = ScanSettings.Builder()
        .setScanMode(CALLBACK_TYPE_FIRST_MATCH)
        .build()

    private val scanCallback = object : ScanCallback() {
        /***
         * 扫描失败的回调
         */
        override fun onScanFailed(errorCode: Int) {
            super.onScanFailed(errorCode)
            MainHandler.post {
                Toast.makeText(activity, "扫描失败", Toast.LENGTH_SHORT).show()
            }
        }

        /***
         * 获取到扫描结果的回调
         */
        @SuppressLint("MissingPermission")
        override fun onScanResult(callbackType: Int, result: ScanResult?) {
            super.onScanResult(callbackType, result)
            Log.e("tag", result?.device!!.address)
             if (result?.device != null && !deviceList.contains(result?.device)) {
                if (result?.device!!.name != null && isWeHealthDevice(
                        result?.device!!.name,
                        true
                    )
                ) {

                    deviceList.add(result?.device!!)
                }
            }
        }

        /***
         * 批量结果产生的回调
         */
        override fun onBatchScanResults(results: MutableList<ScanResult>?) {
            super.onBatchScanResults(results)
        }

    }


    fun getBleDeviceList(): List<BluetoothDevice> {
        return deviceList;
    }

    @SuppressLint("MissingPermission")
    override fun discoverDevice() {
        scanFilterList.add(scanFilter)
        if (!scanning) {
            MainHandler.postDelay(SCAN_PERIOD) {
                scanning = false
                bluetoothAdapter.bluetoothLeScanner.stopScan(scanCallback)
            }
            scanning = true

            bluetoothAdapter.bluetoothLeScanner.startScan(
//                scanFilterList,
//                scanSettings,
                scanCallback
            )
        } else {
            scanning = false
            bluetoothAdapter.bluetoothLeScanner.stopScan(scanCallback)
        }

    }

    @SuppressLint("MissingPermission")
    override fun stopDiscoverDevice() {
        bluetoothAdapter.bluetoothLeScanner.stopScan(scanCallback)
    }

    override fun connectDevice(
        device: BluetoothDevice,
        ecgDataGetListener: ParseOriginEcgData.EcgDataGetListener
    ): LiveData<String> {
        TODO("Not yet implemented")
    }
}